<?php

namespace App\Http\Requests;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

/**
 * 套餐
 *
 * @Desc:验证请求类
 *
 * @author: zhx
 *
 * @Time: 2023/8/10 10:37
 */
class SetmealRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * 获取应用于请求的验证规则。
     *
     * @return array<string, \Illuminate\Contracts\Validation\Rule|array|string>
     */
    public function rules()
    {
        $id = $this->route('setmeal');

        return [
            'app_id' => 'required',
            'correlation' => 'string|max:255',
            'name' => [
                'required',
                'string',
                'max:255',
                Rule::unique('admin_setmeal', 'name')->ignore($id),
            ],
            'description' => 'required|string|max:255',
            'theme_colors' => 'required|string|max:255',
            'fonts_color' => 'required|string|max:255',
            'introduction' => 'required|string|max:255',
            'numeral' => 'required|integer',
            'year_unit' => 'required|in:1,2',
            'price' => 'required|numeric',
            'renewal_fee' => 'required|numeric',
            'how' => 'required|in:0,1',
            'state' => 'required|in:0,1',
        ];
    }

    public function messages(): array
    {
        return [
            'app_id.required' => '应用 ID 字段是必填的。',
            'correlation.required' => '关联功能字段是必填的。',
            'correlation.string' => '关联功能必须是字符串。',
            'correlation.max' => '关联功能不能超过 :max 个字符。',
            'name.required' => '套餐名称字段是必填的。',
            'name.string' => '套餐名称必须是字符串。',
            'name.max' => '套餐名称不能超过 :max 个字符。',
            'name.unique' => '该套餐名称已经被使用。',
            'description.required' => '套餐说明字段是必填的。',
            'description.string' => '套餐说明必须是字符串。',
            'description.max' => '套餐说明不能超过 :max 个字符。',
            'theme_colors.required' => '主题颜色字段是必填的。',
            'theme_colors.string' => '主题颜色必须是字符串。',
            'theme_colors.max' => '主题颜色不能超过 :max 个字符。',
            'fonts_color.required' => '字体颜色字段是必填的。',
            'fonts_color.string' => '字体颜色必须是字符串。',
            'fonts_color.max' => '字体颜色不能超过 :max 个字符。',
            'introduction.required' => '套餐简介字段是必填的。',
            'introduction.string' => '套餐简介必须是字符串。',
            'introduction.max' => '套餐简介不能超过 :max 个字符。',
            'numeral.required' => '数字时间字段是必填的。',
            'numeral.integer' => '数字时间必须是整数。',
            'year_unit.required' => '单位字段是必填的。',
            'year_unit.in' => '单位必须是 1（年）或 2（月）。',
            'price.required' => '价格字段是必填的。',
            'price.numeric' => '价格必须是数字。',
            'renewal_fee.required' => '续费金额字段是必填的。',
            'renewal_fee.numeric' => '续费金额必须是数字。',
            'how.required' => '购买方式字段是必填的。',
            'how.in' => '购买方式必须是 0（联系客服）或 1（在线支付）。',
            'state.required' => '状态字段是必填的。',
            'state.in' => '状态必须是 0（禁用）或 1（正常）。',
        ];
    }

    public function all($keys = null)
    {
        $data = parent::all($keys);

        return $data;
    }

    protected function failedValidation(Validator $validator)
    {
        $error = $validator->errors()->all();
        throw new HttpResponseException(response()->json(['msg' => $error[0], 'code' => '500', 'data' => 'error'], 500));
    }
}
